// cs48 group 7 poker cards nov 2010
// War main

#include "cards.h"
#include "deck.h"
#include <string>
using namespace std;


int main()
{
  srand(time(0)); // part of randomization
  


  Deck warDeck;
  //warDeck.PrintDeck();
  cout << "Welcome to War, press Enter to draw your next card." << endl;
  warDeck.Shuffle();
  Deck playerOne(0), playerTwo(0);
  Deck temporaryDeck(0);
  
  int i = 1; // i is a counter, adds cards 1-26 from deck to player 1's deck
  while (i < 27)
    {
      playerOne.InsertAtBottom(warDeck.DrawCard());
      i++;
    }
  i = 1;
  while(i < 27)
    {
      playerTwo.InsertAtBottom(warDeck.DrawCard());
      i++;
    }

  cout << "Now Starting the Game..." << endl;


  Cards* pOneCard,* pTwoCard;
  while(playerOne.Size() > 0 && playerTwo.Size() > 0){
    pOneCard = playerOne.DrawCard();
    pTwoCard = playerTwo.DrawCard();
    cout << "You drew: " << pOneCard->GetSuit() <<  " " <<pOneCard->GetValue() << " and He Drew: " << pTwoCard->GetSuit() << " " << pTwoCard->GetValue() << endl;
    if(pOneCard->GetValue() > pTwoCard->GetValue()) // if player 1's card has a higher value, add both cards to the bottom of his deck
      {
	playerOne.InsertAtBottom(pOneCard);
	playerOne.InsertAtBottom(pTwoCard);
	cout << "Both cards added to your deck!" << endl;

      }
    else if (pOneCard->GetValue() < pTwoCard->GetValue())
      {
	playerTwo.InsertAtBottom(pTwoCard);
	playerTwo.InsertAtBottom(pOneCard);
	cout << "Both cards added to his deck..." << endl;
      }
    else 
      {
	cout << "War!" << endl;

	//cout << playerOne.Size() << endl;
	//cout << playerTwo.Size() << endl;

	//cin; // use for autoplay
	cin.get(); //use for manual play
	bool war = true;
	while(war){
	temporaryDeck.InsertAtBottom(pOneCard);
	temporaryDeck.InsertAtBottom(pTwoCard);

	if (playerOne.Size() > 2 && playerTwo.Size()> 2) // checks if players have more cards to play
	  {
	    pOneCard = playerOne.DrawCard();
	    temporaryDeck.InsertAtBottom(pOneCard);
	    pTwoCard = playerTwo.DrawCard();
	    temporaryDeck.InsertAtBottom(pTwoCard);
	    cout << "You drew: " << pOneCard->GetSuit() << " " << pOneCard->GetValue() << " and He Drew: " << pOneCard->GetSuit() << " " << pTwoCard->GetValue() << endl;
	    
	    pOneCard = playerOne.DrawCard();
	    temporaryDeck.InsertAtBottom(pOneCard);
	    pTwoCard = playerTwo.DrawCard();
	    temporaryDeck.InsertAtBottom(pTwoCard);
	    cout << "You drew: " << pOneCard->GetSuit() <<  " " <<pOneCard->GetValue() << " and He Drew: " << pOneCard->GetSuit() <<  " " <<pTwoCard->GetValue() << endl;
	    
	    pOneCard = playerOne.DrawCard();
	    temporaryDeck.InsertAtBottom(pOneCard);
	    pTwoCard = playerTwo.DrawCard();
	    temporaryDeck.InsertAtBottom(pTwoCard);
	    cout << "You drew: " << pOneCard->GetSuit() <<  " " <<pOneCard->GetValue() << " and He Drew: " << pOneCard->GetSuit() <<  " " <<pTwoCard->GetValue() << endl;
	  
	    if (pOneCard->GetValue() > pTwoCard->GetValue())
	      {
		cout << "You won the war!" << endl;
		int c = 0;
		while (c < temporaryDeck.Size()){
		playerOne.InsertAtBottom(temporaryDeck.DrawCard());
		c++;
		}
		war = false;
	      }
	    else if (pOneCard->GetValue() < pTwoCard->GetValue())
	      {
		cout << "He won the war..." << endl;
		int c = 0;
		while (c < temporaryDeck.Size()){
		playerTwo.InsertAtBottom(temporaryDeck.DrawCard());
		c++;
		}
		war = false;
	      }
	    //else war = false;
	    //delete temporaryDeck;
	  }
	}
	
	
	
      }
    //cin; //use for autoplay
    cin.get(); // use for manual play
  }
  
  if (playerOne.Size() > 0 )
    {
      cout << "Congrats, you won the war." << endl;
    }
  else
    {
      cout << "Sorry, you lost..." << endl;
    }
  //delete temporaryDeck;
  return 0;





}





